package lookie.swing.tree.model;

import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;

public class LazyTreeNodeModel extends DefaultTreeModel {

	private static final long serialVersionUID = 885272490397068032L;
	
	private TreeNodeProvider provider;
	
	public LazyTreeNodeModel(TreeNode root, TreeNodeProvider provider) {
		super(root);
		this.provider = provider;
	}

	public TreeNodeProvider getProvider() {
		return provider;
	}

	@Override
	public int getChildCount(Object parent) {
		if (parent instanceof LazyTreeNode) {
			LazyTreeNode node = (LazyTreeNode) parent;
			if (!node.isLoaded()) {
				node.fill(provider.getChildren(node));
			}
		}
		return super.getChildCount(parent);
	}

}
